package com.ansion.commonchat.startup.workmanager.bwork.impl.mode;

import androidx.annotation.NonNull;

/**
 * Created by wanghongjie on 2023/5/5
 * Describe :  Work 的数据管理类，等同于 WorkDatabase ，只不过不存入数据库，仅用缓存。
 * 这也是封装BWorkManager的目的，移除数据库相关，仅做缓存。
 * 提供等价的API，模拟数据存储。
 */
public final class WorkDataCacheImpl extends WorkDataCache {
    private volatile BWorkSpecDao _workSpecDao;
    private volatile BDependencyDao _dependencyDao;
    private volatile BWorkTagDao _workTagDao;
    private volatile BWorkCallbackDao _workCallbackDao;

    public WorkDataCacheImpl() {
    }

    @NonNull
    @Override
    public BWorkSpecDao workSpecDao() {
        if (this._workSpecDao != null) {
            return this._workSpecDao;
        } else {
            synchronized (this) {
                if (this._workSpecDao == null) {
                    this._workSpecDao = new BWorkSpecImpl(this);
                }

                return this._workSpecDao;
            }
        }
    }

    @NonNull
    @Override
    public BDependencyDao dependencyDao() {
        if (this._dependencyDao != null) {
            return this._dependencyDao;
        } else {
            synchronized (this) {
                if (this._dependencyDao == null) {
                    this._dependencyDao = new BDependencyImpl(this);
                }

                return this._dependencyDao;
            }
        }
    }

    @NonNull
    @Override
    public BWorkTagDao workTagDao() {
        if (this._workTagDao != null) {
            return this._workTagDao;
        } else {
            synchronized (this) {
                if (this._workTagDao == null) {
                    this._workTagDao = new BWorkTagImpl(this);
                }

                return this._workTagDao;
            }
        }
    }

    @NonNull
    @Override
    public BWorkCallbackDao workCallbackDao() {
        if (this._workCallbackDao != null) {
            return this._workCallbackDao;
        } else {
            synchronized (this) {
                if (this._workCallbackDao == null) {
                    this._workCallbackDao = new BWorkCallbackImpl(this);
                }

                return this._workCallbackDao;
            }
        }
    }

    @Override
    public void logout() {
        workSpecDao().logout();
        dependencyDao().logout();
        workTagDao().logout();
    }

}